package com.zhouke.certificate.controller;

import com.zhouke.certificate.common.Constants;
import com.zhouke.certificate.entity.Dict;
import com.zhouke.certificate.mapper.DictMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zhouke.certificate.common.Result;

import com.zhouke.certificate.service.IMenuService;
import com.zhouke.certificate.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周科
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private IMenuService menuService;

    @Resource
    private DictMapper dictMapper;

    //新增或更新
    @PostMapping
    public Result save(@RequestBody Menu menu) {  // 返回 Boolean
        return Result.success(menuService.saveOrUpdate(menu));
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) { //返回 Boolean
        return Result.success(menuService.removeById(id)); // table.entityPath= user
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){  //返回 Boolean
        return Result.success(menuService.removeByIds(ids));
    }

    //查询所有
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name,
                          @RequestParam(defaultValue = "") String description) { //返回 List<Menu>
        return Result.success(menuService.findMenus(name,description));
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) { // 返回 Menu
        return Result.success(menuService.getById(id));
    }
    //查询图标
    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(null));
    }

}

